
**** birth-level data
use ../data/census1990child, clear
g int wave = 1990

append using ../data/census1982child
replace wave = 1982 if missing(wave)

rename hhcode hhcode_raw
egen hhcode = group(hhcode_raw wave)
drop hhcode_raw


* province
drop if prov == 54 
* drop Tibet

tab prov wave
replace prov = 51 if prov == 50 
* merge Chongqing with Sichuan

replace prov = 44 if prov == 46 
* merge Hainan with Guangdong

* filter households
keep if nfather <= 1 & nmother <= 1

keep if nmother == 1


* han households
g hanFamily = hanP2
replace hanFamily = 0 if hanP1 == 0
tab hanFamily, m
drop if missing(hanFamily)

* extra twins
drop if ntwin > 2
drop if ntriplet > 0
drop if nquadruplet == 4


* coresidence
drop if sampleChCoresideMoreThanBirth == 1

keep if sampleChCoreside == 1


* urban/rural 
tab hukouP2 wave, m

g urban90 = hukouP2 if wave == 1990
replace urban90 = hukouP1 if wave == 1990 & missing(urban90)

g urban82 = 1 - agriempP2 if wave == 1982
replace urban82 = 1 - agriempP1 if wave == 1982 & missing(urban82)


tab urban90 if wave == 1990,m
tab urban82 if wave == 1982,m

g urban = urban90
replace urban = urban82 if wave == 1982
tab urban, m

drop if missing(urban)


* sample restriction by maternal age and child age
drop if missing(birthy)
keep if birthyP2 >= 1940 & birthyP2 <= 1960
drop if age1birthP2 <= 15
keep if ageC1 <= 17
count if ageC1 < 13

* adjust birth order
tab twinhh
tab twin

tab birthorder

g birthorder_alt = birthorder
replace birthorder_alt = birthorder - 1 if birthorder > twinorder & twinhh == 1

replace birthorder_alt = 3 if birthorder_alt > 3


* drop the one twin child
tab twin
drop if birthorder ~= twinorder & twin == 1

*
local kvar1 "hhcode prov wave urban twin birthy age birthorder_alt birthorder twinorder twin "
local kvar2 "twinhh hanFamily birthyP2 ageP2 eduyP2 age1birthP2 age2birthP2 age3birthP2 ageC1 ageC2 maleC1  "

sum `kvar1' `kvar2'
keep `kvar1' `kvar2'

save ../temp/temp, replace


** prepare fine data
use ../raw/OCP/ebenstein_fines, clear
rename province prov
keep prov birthyear fine
save ../temp/fine, replace

use ../raw/ProvinceData/chinaProv.dta, clear
keep prov year gdppc growth
rename year birthyear

distinct prov
drop if prov == 46 | prov == 50 | prov == 54
save ../temp/gdppc, replace


** merge with fine
use ../temp/temp, clear

g birthyear = birthy - 1
merge n:1 prov birthyear using ../temp/fine
drop if _merge == 2
tab birthy if _merge == 1
replace fine = 0 if birthy <= 1979

g agebirthP2 = ageP2 - age

g twin100 = twin * 100

g agegap = ageC1 - ageC2

* birth order dummies
tab birthorder_alt, g(bo)

sum bo*

g bo1_fine = bo1 * fine
g bo2_fine = bo2 * fine
g bo3_fine = bo3 * fine

g rural = 1 - urban


tab birthy

g postOCP = birthy >= 1980

g postOCP_hanFamily = postOCP * hanFamily

g fine_hanFamily = fine * hanFamily

sum fine

egen fe_birthywave = group(birthy wave)

save ../data/c8290_misreported_twins, replace

erase ../temp/temp.dta
erase ../temp/fine.dta
erase ../temp/gdppc.dta



***** analysis following HLZ 2016


** Table A2
use ../data/c8290_misreported_twins, clear

est clear
estpost tabstat twin100 rural hanFamily age agebirthP2 bo1 bo2 bo3, by(hanFamily) s(mean sd n) columns(statistics) listwise

esttab using ../output/TableA2_tabMisrepTwinsSum.csv, replace main(Mean) aux(SD) b(2)  nostar unstack noobs nonote nomtitle nonumber 
* reverse the order of columns when creating Table A2 in the online appendix


** Table A3
local ctrl " urban i.eduyP2 agebirthP2 i.prov#c.birthy "
est clear


eststo: reghdfe twin100 fine hanFamily  `ctrl', a(prov fe_birthywave birthorder_alt) cluster(prov)

eststo: reghdfe twin100 fine  `ctrl' if hanFamily == 1, a(prov fe_birthywave birthorder_alt) cluster(prov)

eststo: reghdfe twin100 fine  `ctrl' if hanFamily == 0, a(prov fe_birthywave birthorder_alt) cluster(prov)


eststo: reghdfe twin100 fine  `ctrl' if hanFamily == 1 & urban == 1, a(prov fe_birthywave birthorder_alt) cluster(prov)

eststo: reghdfe twin100 fine  `ctrl' if hanFamily == 1 & urban == 0, a(prov fe_birthywave birthorder_alt) cluster(prov)


eststo: reghdfe twin100 hanFamily postOCP_hanFamily  `ctrl', a(prov fe_birthywave birthorder_alt) cluster(prov)


local kvars "fine fine_hanFamily postOCP_hanFamily "
esttab using ../output/TableA3_tabMisrepTwinsPolicy.csv, ///
keep(`kvars') /// 
order(`kvars') ///
b(3) se(3) star(* .10 ** .05 *** .01) stats(N r2, labels("Observations"  "R-squared" ) fmt(0 3)) ///
title("Age of the first-born child: 0-17 ") nogaps label n replace



** Table A4
local ctrl " urban i.eduyP2 age1birthP2 i.prov#c.birthy "
est clear
eststo: reghdfe agegap c.postOCP#c.twin twin hanFamily `ctrl' if birthorder_alt == 2 , a(prov fe_birthywave) cluster(prov)

eststo: reghdfe agegap c.postOCP#c.twin twin  `ctrl' if birthorder_alt == 2 & hanFamily == 1, a(prov fe_birthywave) cluster(prov)

eststo: reghdfe agegap c.postOCP#c.twin twin  `ctrl' if birthorder_alt == 2 & hanFamily == 0, a(prov fe_birthywave) cluster(prov)

local kvars "c.postOCP#c.twin twin "
esttab using ../output/TableA4_tabMisrepTwinsAgeGap.csv, ///
keep(`kvars')  /// 
order(`kvars') ///
b(3) se(3) star(* .10 ** .05 *** .01) stats(N r2, labels("Observations"  "R-squared" ) fmt(0 3)) ///
title("") nogaps label n replace


